//
// Created by user on 2022/3/4.
//
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;
int lengthOfLongestSubstring1(string s) {

    int n = s.length();
    if (n <= 1) return n;

    int maxLen = 0;
    int left = 0, right = 0;
    vector<int> window(128, 0);
    while (right < n) {
        char currChar = s[right];
        int currCharIndex = window[currChar];
        left = max(left, currCharIndex);
        maxLen = max(maxLen, right - left + 1);

        window[currChar] = right + 1;
        right++;
    }
    return maxLen;

}

int lengthOfLongestSubstring(string s) {
    int n = s.length();
    if(n<=1){return n;}

    int maxLen =0;
    int left = 0;
    int right = 0;
    unordered_set<char> window;
    while (right<n){
        char currChar = s[right];
        if(!window.count(currChar)){
           maxLen= max(maxLen, right-left+1);
           window.insert(currChar);
           right++;
        }else{
            window.erase(s[left]);
            left++;
        }
    }
    return maxLen;
}